﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Security.Cryptography;
using Newtonsoft.Json.Linq;
using _80X_CG_Frame.Workers;
using System.IO;
using _80X_CG_Frame.Objects;

namespace _80X_CG_Frame.Windows
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class Login : Window
    {
        public  JObject user_info;
        public Login()
        {
            InitializeComponent();
            var ui = GLOBAL.config.configs["user_info"];
            if (ui == null)
            {
                user_info = new JObject();
                var admin = new JObject();
                admin["password"] = Encrypt("admin");
                admin["right"] = Right.Admin.ToString();
                user_info["Admin"] = admin;
                var op = new JObject();
                op["password"] = Encrypt("123");
                op["right"] = Right.Operator.ToString();
                user_info["Operator"] = op;
                GLOBAL.config.configs["user_info"] = EncryptJson(user_info.ToString());
                GLOBAL.config.WriteConfig();
            }
            else
            {
                user_info = (JObject)JObject.Parse(DecryptJson((string)ui));
            }

            LoadUserInfo();
            password.Focus();
        }
        private void LoadUserInfo()
        {
            username.Items.Clear();
            foreach (var t in user_info)
            {
                username.Items.Add(t.Key);
            }
            if (username.Items.Count != 0)
                username.SelectedIndex = 0;
        }

        /// <summary>
        /// 加密json
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static string EncryptJson(string json)
        {
            return Convert.ToBase64String(Encoding.UTF8.GetBytes(json));
        }
        /// <summary>
        /// 解密json
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string DecryptJson(string str)
        {
            return Encoding.UTF8.GetString(Convert.FromBase64String(str));
        }

        /// <summary>
        /// 加密密码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Encrypt(string str)
        {
            var sha = new SHA256Managed();
            var base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(str));
            byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(base64));
            string concat = "";
            foreach (var b in hash)
            {
                concat += b.ToString("X2");
            }
            return concat;
        }

        /// <summary>
        /// 验证密码
        /// </summary>
        /// <param name="str"></param>
        /// <param name="str_hash"></param>
        /// <returns></returns>
        public static bool Vertify(string str, string str_hash)
        {
            if (str_hash == Encrypt(str))
                return true;
            else
                return false;
        }

        private void Click_Login()
        {
            var user = user_info[(string)username.SelectedItem];
            if (user != null)
            {
                var confirm = Vertify(password.Password, (string)user["password"]);
                if (confirm)
                {
                    GLOBAL.user.Name = (string)username.SelectedItem;
                    GLOBAL.user.SetRight((string)user["right"]);
                    this.DialogResult = true;
                    this.Close();
                }
                else
                {
                    warn_msg.Text = "密码错误";
                }
            }
        }

        private void confirm_Click(object sender, RoutedEventArgs e)
        {
            Click_Login();
        }

        private void password_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Enter)
                Click_Login();
        }
    }
}
